<body>
<!-- 正文开始 -->
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <!-- 表格工具栏 -->
            <form class="layui-form toolbar">
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <select name="field_name" lay-verify="required" required>
                            <option value="uid">用户ID</option>
                            <option value="account">账号</option>
                        </select>
                    </div>
                    <div class="layui-inline">
                        <input name="field_value" class="layui-input" placeholder="输入搜索内容"/>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn icon-btn" lay-filter="search" lay-submit>
                            <i class="layui-icon">&#xe615;</i>搜索
                        </button>
                    </div>
                </div>
            </form>
            <!-- 数据表格 -->
            <table id="test" lay-filter="test"></table>
        </div>
    </div>
</div>

<!-- 表格操作列 -->
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-primary layui-btn-xs" lay-event="edit">修改</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<!-- 表单弹窗 -->
<script type="text/html" id="saveDialog">
    <form id="saveForm" lay-filter="saveForm" class="layui-form model-form">
        <input type="hidden" name="id">
        <div class="layui-form-item">
            <label class="layui-form-label">地址
            </label>
            <div class="layui-input-block">
                <input name="address" placeholder="地址最多100个字符" class="layui-input"
                       lay-verType="tips" lay-verify="address" autocomplete="off" required/>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label layui-form-required">网络</label>
            <div class="layui-input-block">
                <select name="network" lay-verify="required" required>
                    <option value="">选择网络</option>
                    <option value="trc20">trc20</option>
                    <option value="erc20">erc20</option>
                </select>
            </div>
        </div>
        <div class="layui-form-item text-right">
            <button class="layui-btn" lay-filter="saveSubmit" lay-submit>保存</button>
            <button class="layui-btn layui-btn-primary" type="button" ew-event="closeDialog">取消</button>
        </div>
    </form>
</script>

<!-- js部分 -->
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/libs/layui/layui.js"></script>
<script type="text/javascript" src="__TPL_RESOURCE_BACKEND__/js/common.js"></script>
<script>
    layui.use(['layer', 'form', 'table', 'admin'], function () {
        var $ = layui.jquery;
        var layer = layui.layer;
        var form = layui.form;
        var table = layui.table;
        var admin = layui.admin;

        /* 表单验证 */
        form.verify({
            address: function (value, item) {
                if (value.length > 100) {
                    return '地址最多100个字符';
                }
            }
        });

        /* 渲染表格 */
        var insTb = table.render({
            elem: '#test',
            url: "{{:url('Usdt/index')}}",
            page: {
                limit: 20
            },
            method: 'post',
            contentType: 'application/json',
            toolbar: true,
            cols: [[
                {field: 'id', title: 'ID'},
                {field: 'user_account_txt', title: '账户'},
                {field: 'address', title: '地址'},
                {field: 'network', title: '网络'},
                {title: '操作', toolbar: '#barDemo', align: 'center', width: 120}
            ]]
        });

        /* 表格搜索 */
        form.on('submit(search)', function (data) {
            insTb.reload({where: data.field, page: {curr: 1}});
            return false;
        });

        /* 表格工具条点击事件 */
        table.on('tool(test)', function (obj) {
            if (obj.event === 'edit') { //修改
                showSaveModel(obj.data);
            } else if (obj.event === 'del') { //删除
                doDel(obj);
            }
        });

        /* 表格头工具栏点击事件 */
        table.on('toolbar(test)', function (obj) {
            if (obj.event === 'add') { // 添加
                showSaveModel();
            }
        });

        /* 显示表单弹窗 */
        function showSaveModel(mData) {
            admin.open({
                type: 1,
                title: (mData ? '修改' : '添加') + 'USDT',
                area: ['500px', '300px'],
                maxmin: true,
                resize: true,
                content: $('#saveDialog').html(),
                success: function (layero, dIndex) {
                    // 回显表单数据
                    form.val('saveForm', mData);
                    var url = mData ? "{{:url('Usdt/edit')}}" : "{{:url('Usdt/add')}}";
                    // 表单提交事件
                    form.on('submit(saveSubmit)', function (data) {
                        var loading = layer.load(3);

                        $.ajax({
                            url: url,
                            type: 'post',
                            data: data.field,
                            success: function (res) {
                                if (res.code != 0) {
                                    layer.msg(res.msg, {icon: 5, time: 1000});
                                } else {
                                    layer.close(dIndex);
                                    layer.msg(res.msg, {icon: 1, time: 1500});
                                    insTb.reload();
                                }
                                layer.close(loading);
                            }, error: function () {
                                layer.msg('网络错误请稍后重试', {icon: 5, time: 1000});
                                layer.close(loading);
                            }
                        });
                        return false;
                    });
                }
            });
        }

        /* 删除 */
        function doDel(obj) {
            layer.confirm('确定要删除选中数据吗？', {
                skin: 'layui-layer-admin',
                shade: .1
            }, function (i) {
                layer.close(i);
                var loading = layer.load(3);
                $.ajax({
                    url: "{{:url('Usdt/del')}}",
                    type: 'post',
                    data: {id: obj.data.id},
                    success: function (res) {
                        if (res.code != 0) {
                            layer.msg(res.msg, {icon: 5, time: 1000});
                        } else {
                            layer.msg(res.msg, {icon: 1, time: 1500});
                            insTb.reload();
                        }
                        layer.close(loading);
                    }, error: function () {
                        layer.msg('网络错误请稍后重试', {icon: 5, time: 1000});
                        layer.close(loading);
                    }
                });
                return false;
            });
        }
    });
</script>
</body>
</html>
